﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using Common.Views;
using ElectrosLtdApplication.Models;

namespace ElectrosLtdApplication.Controllers
{
    public class ProductsController : MenusController
    {
        //
        // GET: /Products/
               
        public ActionResult Index()
        {
            List<Product> products = new ProductsServ.ProductsServiceClient().GetAllProducts().ToList();
            return View("Index", products);
        }
        
        public PartialViewResult GetCommentsForProduct(int productID)
        {
            List<CommentsProduct> comments = new ProductsServ.ProductsServiceClient().GetCommentsForProduct(productID).ToList();
            return PartialView("_comments", comments);
        }

        [Authorize]
        public string AddToCart(int pId, int quantity)
        {
            string value = new ProductsServ.ProductsServiceClient().AddToCart(HttpContext.User.Identity.Name, pId, quantity);
            return value;
        }

        [Authorize]
        public ActionResult ViewCart()
        {
            ProductsModel pm = new ProductsModel(HttpContext.User.Identity.Name);
            List<CartView> products = pm.ProductsList;

            decimal totalPrice = 0;

            foreach (CartView p in products)
            {
                totalPrice += (p.ProductPrice * p.ProductQuantity);
            }

            ViewBag.TotalPrice = totalPrice;
            return View("ViewCart", products);
        }

        [HttpPost]
        public ActionResult ViewCart(ProductsModel data)
        {
            List<CartView> products = new ProductsServ.ProductsServiceClient().GetProductsInShoppingCart(HttpContext.User.Identity.Name).ToList();
            try
            {
                if (products.Count() != 0)
                {
                    data.OrderID = Guid.NewGuid();
                    new OrdersServ.OrdersServiceClient().PlaceOrder(HttpContext.User.Identity.Name, data.OrderID, products);
                    List<Product> allproducts = new ProductsServ.ProductsServiceClient().GetAllProducts().ToList();
                    ViewBag.Message = "Your order has been successfully placed!";
                    return View("ViewCart", null);
                }
                else
                {
                    ViewBag.Message = "There are no products in your shopping cart!";
                    return View("ViewCart", null);
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
                return View("ViewCart", null);
            }
        }

        [Authorize]
        public PartialViewResult GetAverageRatingForProduct(int productID)
        {
            List<int> ratings = new ProductsServ.ProductsServiceClient().GetRatingForProduct(productID).ToList();
            int total = 0;
            
            foreach (int i in ratings)
            {
                total += i;
            }

            double noOfRatings = ratings.Count();
            double average = Math.Round((total / noOfRatings), 2);

            ViewData["averageRating"] = average;
            ViewData["productID"] = productID;

            return PartialView("_rating", average);
        }
    }
}
